package com.company.project;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@MapperScan("com.company.project.mapper")
@EnableTransactionManagement
@Slf4j
public class CompanyProjectApplication {

    private static Map<String, String> dataMap;

    static {
        data();
    }

    private static void data() {
        dataMap = new HashMap<>();
        // 外部静态文件地址
        String path = new String("static/");
        File file = new File(path);
        String pathUrl = file.getAbsolutePath() + "\\";
        dataMap.put("--web.upload-path", pathUrl);
    }

    private static String[] startParame(String[] args) {
        Map<String, String> resMap = new HashMap<>();
        for (int i = 0; i < args.length; i++) {
            String[] val = args[i].split("=");
            resMap.put(val[0], val[1]);
            for (Map.Entry<String, String> entry : resMap.entrySet()) {
                if (!StringUtils.isEmpty(resMap.get(entry.getKey()))) {
                    dataMap.remove(entry.getKey());
                }
            }
        }
        resMap.putAll(dataMap);
        List<String> resList = new ArrayList<String>();
        for (Map.Entry<String, String> entry : resMap.entrySet()) {
            resList.add(entry.getKey()+"="+entry.getValue());
        }
        return resList.toArray(new String[resList.size()]);
    }

    public static void main(String[] args) throws Exception {
        String[] myArgs = startParame(args);
        ConfigurableApplicationContext application = SpringApplication.run(CompanyProjectApplication.class, myArgs);

        Environment env = application.getEnvironment();
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Login: \thttps://{}:{}/login\n\t" +
                        "Doc: \thttps://{}:{}/doc.html\n" +
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"));
    }

}